package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.utils.MathUtils;
import com.baichang.taoshunda.dao.BussinessDao;
import com.baichang.taoshunda.dao.FreezeOrderDao;
import com.baichang.taoshunda.dao.OrderDao;
import com.baichang.taoshunda.dao.OrderDetailsDao;
import com.baichang.taoshunda.entity.*;
import com.baichang.taoshunda.entity.em.OrderState;
import com.baichang.taoshunda.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by ${wushenglu} on 2018/8/10.
 */
@Service
public class TaoshundaServiceImpl implements TaoshundaService {
    @Autowired
    BusinessService businessService;
    @Autowired
    OrderDao orderDao;
    @Autowired
    FreezeOrderDao freezeOrderDao;

    @Autowired
    BussinessDao bussinessDao;

    @Autowired
    private OrderDetailsDao orderDetailsDao;

    @Autowired
    private UserService userService;

    @Autowired
    private PensionService pensionService;

    @Autowired
    private RebateService rebateService;

    @Override
    public void addMoneyCompany(String data) throws ServiceException {
//查询
        List<FreezeOrder> freezeOrderList = freezeOrderDao.getFreezeOrder();
        System.out.println("=============================冻结订单数量" + freezeOrderList.size());
        if (freezeOrderList.size() > 0) {
            for (int i = 0; i < freezeOrderList.size(); i++) {
                Order order = orderDao.getOrderByOrderNumber(freezeOrderList.get(i).getOrderNumber());
                if (order != null) {
                    if (order.getOrderState().equals(OrderState.已完结.getCode()) || order.getOrderState().equals(OrderState.待评价.getCode())) {

//                        businessService.updateBussinessBalance(order.getSellerId(), order.getBussGetMoney().doubleValue(), CompanyBalanceState.订单完成.getCode(), order.getOrderNumber(), order.getRefunds());
                        Bussiness bussiness = businessService.getBusinessById(order.getSellerId());
                        Double money = MathUtils.add(bussiness.getBalance().doubleValue(), order.getBussGetMoney().doubleValue());
                        bussinessDao.updateBussinessBalance(bussiness.getId(), money);
                        //计算冻结金额
                        Double afterMoney = MathUtils.sub(bussiness.getFreezeMoney().doubleValue(), order.getBussGetMoney().doubleValue());
                        //修改商家冻结金额
                        businessService.updateFreezeMoney(order.getSellerId(), afterMoney);
                    }
                    User user = userService.getUserBeanById(order.getBuyerId());
                    if (user != null) {
                        if (user.getIsCertificate() == 1) {
                            OrderDetails orderdetail = orderDetailsDao.getGoodsByOrderNumber(order.getOrderNumber(), 1);
                            int number = orderDetailsDao.getGoodsNUmberByOrderNumber(order.getOrderNumber());
                            String goodsName = "";
                            if (number > 1) {
                                goodsName = orderdetail.getGoodsName() + "等多件商品";
                            } else {
                                goodsName = orderdetail.getGoodsName();
                            }
                            //查询订单详情所有商品金额   1616
                            BigDecimal allGoodsMoney = orderDetailsDao.getAllMoney(order.getOrderNumber());
                            //查询订单详情商家同意退货的金额  580
                            BigDecimal agreeReturnMoney = orderDetailsDao.getReturnMoney(order.getOrderNumber(), 3);
                            pensionService.savePension(order.getBuyerId(), goodsName, 1, orderdetail.getGoodsSpec(), order.getOrderNumber(),
                                    (allGoodsMoney.subtract(agreeReturnMoney)).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_CEILING, 3), order.getAllMoney(),
                                    (allGoodsMoney.subtract(agreeReturnMoney)).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_CEILING, 3), 1,
                                    order.getSellerId(), order.getPayType(), order.getGoodsAdress(), order.getRedPacketMoney(), orderdetail.getGoodsId());
                            rebateService.addRebate(order.getSellerId(), order.getOrderNumber(), 1);
                        }
                    }

                    //解除冻结直接删除
                    freezeOrderDao.deleteById(freezeOrderList.get(i).getId());
                }
                System.out.println("=============================冻结订单数量-------1");
            }


        }

    }
}

